package com.ielts.services;

import java.io.UnsupportedEncodingException;

import org.apache.http.entity.StringEntity;

import vn.com.baseapplication.BaseApplication;
import vn.com.baseapplication.constant.BaseConstant;
import vn.com.baseapplication.service.BaseService;
import android.content.Intent;
import android.os.Build;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.ielts.IeltsConstant;
import com.ielts.activity.IeltsApplication;
import com.ielts.model.User;

public class IeltsServices extends BaseService {
	public String getHelloString() {
		return "hello";
	}

	public void getGuild() {
		String urlGuild = IeltsUrl.URL_GUILD;

		network.get(IeltsConstant.GET_GUILD_FROM_SERVER, this, urlGuild);
	}

	public void registerUserGet() {
		String deviceType = "android";
		String deviceToken = IeltsApplication.getInstance().getDeviceId();
		deviceToken = GcmBackground.getGcmId(this);
		String deviceId = IeltsApplication.getInstance().getDeviceId();
		String latitude = BaseApplication.getInstance().getGPS()[0] + "";
		String longitude = BaseApplication.getInstance().getGPS()[1] + "";
		String osVersion = Build.VERSION.RELEASE;
		String AppVersion = BaseApplication.getInstance().getVersionApp();
		String facebookId = IeltsApplication.getInstance().getValue(IeltsConstant.KEY_FB_LINK_ID, IeltsConstant.KEY_FB_LINK_DEF);
		String custName = IeltsApplication.getInstance().getValue(IeltsConstant.KEY_USER_NAME, IeltsConstant.KEY_USER_NAME_DEF);
		String custEmail = IeltsApplication.getInstance().getValue(IeltsConstant.KEY_USER_EMAIL, IeltsConstant.KEY_USER_EMAIL_DEF);
		
		String url = IeltsUrl.URL_REG_USER
				+ "?deviceType=" + deviceType
				+ "&deviceToken=" + deviceToken
				+ "&deviceId=" + deviceId
				+ "&latitude=" + latitude
				+ "&longitude=" + longitude
				+ "&OSversion=" + osVersion
				+ "&custName=" + custName
				+ "&facebookId=" + facebookId
				+ "&custEmail=" + custEmail
				+ "&AppVersion=" + AppVersion;
		Log.e("REGISTER_USER", url);
		network.get(IeltsConstant.REGISTER_USER, this, url);
	}
	
	public void registerUserPost(){
		String deviceType = "android";
		String deviceToken = IeltsApplication.getInstance().getDeviceId();
		deviceToken = IeltsApplication.getInstance().getValue(IeltsConstant.KEY_GCM_ID, IeltsConstant.KEY_GCM_ID_DEF);
		String deviceId = IeltsApplication.getInstance().getDeviceId();
		String latitude = BaseApplication.getInstance().getGPS()[0] + "";
		String longitude = BaseApplication.getInstance().getGPS()[1] + "";
		String osVersion = Build.VERSION.RELEASE;
		String AppVersion = BaseApplication.getInstance().getVersionApp();
		String facebookId = IeltsApplication.getInstance().getValue(IeltsConstant.KEY_FB_LINK_ID, IeltsConstant.KEY_FB_LINK_DEF);
		String custName = IeltsApplication.getInstance().getValue(IeltsConstant.KEY_USER_NAME, IeltsConstant.KEY_USER_NAME_DEF);
		String custEmail = IeltsApplication.getInstance().getValue(IeltsConstant.KEY_USER_EMAIL, IeltsConstant.KEY_USER_EMAIL_DEF);
		try {
			JsonObject jsonParam = new JsonObject();
			jsonParam.addProperty("deviceType", deviceType);
			jsonParam.addProperty("deviceToken", deviceToken);
			jsonParam.addProperty("deviceId", deviceId);
			jsonParam.addProperty("latitude", latitude);
			jsonParam.addProperty("longitude", longitude);
			jsonParam.addProperty("OSversion", osVersion);
			jsonParam.addProperty("custName", custName);
			jsonParam.addProperty("facebookId", facebookId);
			jsonParam.addProperty("custEmail", custEmail);
			jsonParam.addProperty("AppVersion", AppVersion);
			Log.e("String send to server", jsonParam.toString()+"");
			StringEntity entity = new StringEntity(jsonParam.toString());
			network.post(IeltsConstant.REGISTER_USER, IeltsUrl.URL_REG_USER, entity);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
	}

	public void getUserBalance() {
		String userId = IeltsApplication.getInstance().getValue(IeltsConstant.KEY_USER_ID, IeltsConstant.KEY_USER_ID_DEF);
		if (userId.equals(IeltsConstant.KEY_USER_ID_DEF))
			return;
		String url = IeltsUrl.URL_BALANCE
				+ "custId=" + userId;
		network.get(IeltsConstant.GET_BALANCE, getApplicationContext(), url);
	}

	@Override
	public void onNetSuccess(int requestId, String response) {
		switch (requestId) {
		case IeltsConstant.GET_GUILD_FROM_SERVER:
			Intent intent = new Intent();
			intent.putExtra(BaseConstant.MSG_FLAG, IeltsConstant.GET_GUILD_FROM_SERVER + "");
			intent.putExtra(BaseConstant.MSG_DATA, response);
			sendBaseBroadcast(this, intent);

			network.setCache(IeltsUrl.URL_GUILD, response);
			break;
		case IeltsConstant.REGISTER_USER:
			Gson gson = new Gson();
			Log.e("REGISTER_USER", response + "");
			User user = gson.fromJson(response, User.class);
			IeltsApplication.getInstance().setValue(IeltsConstant.KEY_USER_ID, user.Result.custId);
			IeltsApplication.getInstance().setValue(IeltsConstant.KEY_USER_BALANCE, user.Result.booId);

			Intent intentStartMainActivity = new Intent();
			intentStartMainActivity.putExtra(BaseConstant.MSG_FLAG, IeltsConstant.REGISTER_USER + "");
			intentStartMainActivity.putExtra(BaseConstant.MSG_RESULT, BaseConstant.MSG_RESULT_OK);
			sendBaseBroadcast(this, intentStartMainActivity);
			break;
		case IeltsConstant.GET_BALANCE:
			Log.e("BALANCE", response + "");
			Gson gsonUserBalance = new Gson();
			User userBalance = gsonUserBalance.fromJson(response, User.class);
			if ("1".equals(userBalance.result)) {
				IeltsApplication.getInstance().setValue(IeltsConstant.KEY_USER_BALANCE, userBalance.CustBalance.balance);
				IeltsApplication.getInstance().setValue(IeltsConstant.KEY_USER_BUY_QTY, userBalance.CustBalance.buyQty);
				IeltsApplication.getInstance().setValue(IeltsConstant.KEY_USER_REMAIN, userBalance.CustBalance.remainQty);

				Intent intentBalance = new Intent();
				intentBalance.putExtra(BaseConstant.MSG_FLAG, IeltsConstant.GET_BALANCE + "");
				intentBalance.putExtra(BaseConstant.MSG_DATA, response);
				sendBaseBroadcast(this, intentBalance);
			}

			break;
		default:
			break;
		}
	}

	@Override
	public void onNetSuccessCache(int requestId, String response) {
		super.onNetSuccessCache(requestId, response);
	}

	@Override
	public void onNetLoading(int requestId) {
		super.onNetLoading(requestId);
	}

	@Override
	public void onNetTimeOut(int requestId, Throwable error) {
		switch (requestId) {
		default:
			break;
		}
	}

	@Override
	public void onNetFailed(int requestId, String error) {
		switch (requestId) {
		case IeltsConstant.REGISTER_USER:
			Intent intentStartMainActivity = new Intent();
			intentStartMainActivity.putExtra(BaseConstant.MSG_FLAG, IeltsConstant.REGISTER_USER + "");
			intentStartMainActivity.putExtra(BaseConstant.MSG_RESULT, BaseConstant.MSG_RESULT_FAILED);
			sendBaseBroadcast(this, intentStartMainActivity);
			break;
		default:
			break;
		}
	}
}
